package com.baizhi.springbootshiro.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.baizhi.springbootshiro.realm.CustomerRealm;
import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import com.google.common.collect.Maps;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;

/**
 * shiro配置
 *
 * @author Administrator
 */
@Data
@Configuration
@EnableConfigurationProperties
public class ShiroConfig {
    ShiroProperties shiroProperties;

    /**
     * 自定义authorizingRealm
     *
     * @return
     */

    @Bean
    public CustomerRealm customerRealm() {
        CustomerRealm realm = new CustomerRealm();
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
        matcher.setHashAlgorithmName("md5");
        matcher.setHashIterations(1024);
        realm.setCredentialsMatcher(matcher);
        //开启全局缓存
        //设置缓存管理器
        //此处有bug

//        realm.setCachingEnabled(false);
        //认证缓存
//        realm.setAuthenticationCachingEnabled(true);
//        realm.setAuthenticationCacheName(".authenticationCache");
//        授权缓存
//        realm.setAuthorizationCachingEnabled(true);
//        realm.setAuthorizationCacheName(".authorizationCache");
//        realm.setCacheManager(new RedisCacheManager());

        return realm;
    }


    /**
     * 创建安全管理器
     *
     * @param customerRealm customerRealm
     * @return DefaultWebSecurityManager
     */
    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(CustomerRealm customerRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(customerRealm);
        return securityManager;
    }

    /**
     * 设置Shiro Filter
     *
     * @param securityManager securityManager
     * @return ShiroFilterFactoryBean
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //设置安全管理器
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //设置受限路径
        HashMap<String, String> map = Maps.newHashMap();
        //设置放行的路径
        shiroProperties.getUrls().forEach(t -> map.put(t, shiroProperties.getType()));
        map.put("/**", "authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        shiroFilterFactoryBean.setLoginUrl("/login");
        shiroFilterFactoryBean.setSuccessUrl("/index");
        return shiroFilterFactoryBean;
    }


    //配置验证码图片实例
    @Bean
    public Producer producer() {
        Properties properties = new Properties();
        properties.put("kaptcha.border", "no");
        properties.put("kaptcha.textproducer.char.space", "5");
        properties.put("kaptcha.textproducer.char.length", "4");
        properties.put("kaptcha.textproducer.font.names", "Arial,Courier,cmr10,宋体,楷体,微软雅黑");
        Config config = new Config(properties);
        DefaultKaptcha kaptcha = new DefaultKaptcha();
        kaptcha.setConfig(config);
        return kaptcha;
    }

    //shiro页面解析

    @Bean
    public ShiroDialect shiroDialect() {
        return new ShiroDialect();
    }

}
